using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Ltf.Diffs.MergeCommands;
using Ltf.Remoting.TFAccess;

namespace Ltf.Diffs
{
	public enum DifferenceInfoRenameType
	{
		Renamed,
		Moved
	}

	[Flags]
	public enum FileChanges
	{
		Content = 1,
		Path = 2,
		Added = 4,
		Deleted = 8,
		UnDeleted = 16,
		Conflicts = 32
	}

	public class DifferenceInfo
	{
		public FileChanges FileChanges;
		public string ChangeFromPath;
		public string ChangeToPath;
		public string TargetPath;

		public string OldName;
		public string NewName;
		public string ItemTargetPath;

		public DifferenceInfoRenameType RenameType;

		public string DiffResult;

		public int Inserts, Changes, Deletes, Conflicts = 0;
		public int ExistingInserts, ExistingChanges, ExistingDeletes = 0;

		public bool RenameConflict = false;
		public bool RenameExisting = false;


		public int AllChanges = 0;
		public int AllExisting = 0;

		public bool Resolved = false;

		public MergeCommandsCollection MergeCommands = new MergeCommandsCollection();


		public DifferenceInfo(string newName, string oldName, FileChanges fileChanges, string changeFromPath, string changeToPath, string targetPath, string diffResult)
		{
			FileChanges = fileChanges;
			ChangeFromPath = changeFromPath;
			ChangeToPath = changeToPath;
			TargetPath = targetPath;
			DiffResult = diffResult;

			NewName = newName;
			OldName = oldName;

			ItemTargetPath = string.Empty;
			string itemTargetName = ((newName == string.Empty) ? oldName : newName);
			ItemTargetPath = LizardFileAccess.PathConcat( targetPath, itemTargetName);

			Diff3Results d3Results = new Diff3Results(DiffResult);

			d3Results.GetHunkCounts(out Inserts, out Changes, out Deletes, out Conflicts,
				out ExistingInserts, out ExistingChanges, out ExistingDeletes);

			AllChanges = Inserts + Changes + Deletes + Conflicts;
			AllExisting = ExistingChanges + ExistingDeletes + ExistingInserts;

		}

		public void BuildCounts(List<string> oldNames, List<string> newNames, List<string> renamesDone)
		{

			if (AllChanges != 0 || AllExisting != 0)
			{

				string fullOldName =  LizardFileAccess.PathConcat( ChangeFromPath, OldName);
				string targetNewName = string.Empty;
				string targetOldName = string.Empty;

				if (oldNames.Contains(fullOldName))
				{
					//OldName = GetPathEnd(ChangeFromPath, OldName);
					//NewName = GetPathEnd(ChangeToPath, newNames[oldNames.IndexOf(OldName)]);
					renamesDone.Add(fullOldName);
					targetNewName = LizardFileAccess.PathConcat(TargetPath,NewName);
					targetOldName = LizardFileAccess.PathConcat(TargetPath,OldName);
					bool newTargetExists = File.Exists(targetNewName);
					bool oldTargetExists = File.Exists(targetOldName);
					RenameType = (Path.GetDirectoryName(targetOldName) == Path.GetDirectoryName(targetNewName)) ? DifferenceInfoRenameType.Renamed : DifferenceInfoRenameType.Moved;
					if (newTargetExists && oldTargetExists)
					{
						RenameConflict = true;
						Conflicts = -(Conflicts + 1);
					}
					else if (newTargetExists)
					{
						RenameExisting = true;
						Conflicts = -(Conflicts + 1);
					}
				}


			}
		}

		private string GetPathEnd(string basePath, string path)
		{
			string result = path.Substring(basePath.Length + 1);
			return result;
		}
	}
}
